//
//  FLMThreeJumpView.m
//  FLMSuperS
//
//  Created by xiaoyang on 16/9/12.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMThreeJumpView.h"
#import "FLMHomeResponseModel.h"
#import "UIImageView+WebCache.h"

#define KJumpTag 100

@interface FLMThreeJumpView ()

@property (strong,nonatomic)NSArray *images;

@property (strong, nonatomic) FLMHomeMsgModel *uniosModelArr;

@end

static const CGFloat gap = 0;
static const NSInteger col = 5;

@implementation FLMThreeJumpView


-(NSArray *)images
{
    if (!_images)
    {
        _images = [[NSArray alloc]init];
    }
    return _images;
}


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
    }
    return self;
}


- (void)configViewWithArray:(NSArray *)datas
{
    
    self.images = datas;
    for (UIImageView *subs in self.subviews) {
        [subs removeFromSuperview];
    }
    
    
    CGFloat x = gap;
    CGFloat y = gap/5;
    CGFloat imageWidth = (ScreenW - gap * (col+1))/col;
    CGFloat imageHeight = imageWidth;
    
    for (NSInteger i=0 ; i < datas.count; i++) {
        FLMUnionsModel *unionModel = datas[i];
       
        UIImageView *imageV = [[UIImageView alloc]init];
        imageV.frame = CGRectMake(x, y, imageWidth,imageHeight);
        imageV.userInteractionEnabled = YES;
        [imageV sd_setImageWithURL:[NSURL URLWithString:unionModel.logourl] placeholderImage:[UIImage imageNamed:@"backView"]];
        
        if ((i+1) % col == 0 && i !=0)
        {
            x = gap;
            y += imageHeight + gap;
        }
        else
        {
            x += imageWidth + gap;
        }
        imageV.tag = KJumpTag + i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [imageV addGestureRecognizer:tap];
        
        [self addSubview:imageV];
        

    }
}

+ (CGFloat)getItemHeight:(NSArray *)datas
{
     CGFloat itemHeight = (ScreenW - gap * (col+1))/col;
     CGFloat rows = (datas.count + col -1)/col;
     return rows * itemHeight;
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    FLMUnionsModel *modle = self.images[tap.view.tag - KJumpTag];
    if (self.imageClickBlock) {
        self.imageClickBlock(modle);
    }
    
}

@end
